import { h, ref, unref, reactive } from 'vue'
import { findDictBrand,findDictTypeList} from '@/api/common'
import { dictType,mountStatusType} from '@/utils/dict'
import { dataToSelect} from '@/utils'
import { Edit,} from '@element-plus/icons-vue'
import { getDroneList} from '@/api/resource-manage'

let router:any = null

/**
 *  获取路由对象
 */
export function getRouterInstance(r) {
  router = r
}

let DT:any = null


// 品牌
const airportBrand = ref<dictType[]>([]) 
// 无人机型号
const airportModelCode = ref<dictType[]>([]) 
// 无人机状态
const airportStatus = ref<dictType[]>([]) 


// 获取data对象
export function getDataInstance(data) {
  DT = data
}

interface optionsType{
  label:string
  value:string | number | null | undefined
}
interface formItemType {
  type: string,
  key: string,
  label: string,
  row: object,
  props: object
  options?: optionsType[]
}

interface OnionsType {
  modalForm:any
  formItem: formItemType[]
  modalRules?:object
}

// 基础信息
export const baseOpions = reactive({
  modalForm: {
    code:null, // 机库编号
    brand:null, // 机库品牌
    modelCode:null, // 机库型号
    onLineStatus:0, // 1在线 0不在线
    status:null, // 机库状态(1可用 0不可用 2报废)
    name:null, // 机库名称
    coord:null, // 坐标
    longitude:null, // 经度
    latitude:null, // 纬度
    pointCoord:null, // 备降坐标
    pointLongitude:null, // 备降点经度
    pointLatitude:null, // 备降点纬度
    internalMonitorUrl:null, // 内部监控地址
    externalMonitorUrl:null, // 外部监控地址
  },
  formItem: [
    {
      type: 'input',
      key: 'code',
      label: '机库编码',
      row: { sm: 8, md: 8 },
      props: { maxlength: '50', clearable: true, placeholder: '请输入机库编码' }
    },
    {
      type: 'select',
      key: 'brand',
      label: '品牌',
      options: airportBrand,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'select',
      key: 'modelCode',
      label: '机库型号',
      options: airportModelCode,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'select',
      key: 'status',
      label: '可用状态',
      options: airportStatus,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'name',
      label: '机库名称',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请输入机库名称' }
    },
    {
      type: 'input',
      key: 'coord',
      label: '经纬度',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'pointCoord',
      label: '备降点',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'internalMonitorUrl',
      label: '内部监控地址',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
    {
      type: 'input',
      key: 'externalMonitorUrl',
      label: '外部监控地址',
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' }
    },
  ],
  modalRules: {
    code: [{ required: true, message: '请输入机库编号', trigger: ['change'] }],
    brand: [{ required: true, message: '请选择机库品牌', trigger: ['change'] }],
    modelCode: [{ required: true, message: '请选择机库型号', trigger: ['change'] }],
    status: [{ required: true, message: '请选择可用状态', trigger: ['change'] }],
    name: [{ required: true, message: '请输入机库名称', trigger: ['change'] }],
    coord: [{ required: true, message: '请选择坐标', trigger: ['change'] }],
    pointCoord: [{ required: true, message: '请选择备降坐标', trigger: ['change'] }],
  },
})


// 挂载类型
const mountType = ref<dictType[]>([]) 
// 品牌
const mountBrand = ref<dictType[]>([]) 

// 无人机品牌
const droneBrand=   ref<dictType[]>([]) 
// 无人机型号
const droneModelCode=   ref<dictType[]>([]) 
// 无人机
const droneList =   ref<dictType[]>([]) 

// 云台参数
export const otherOpions= reactive({
  modalForm: {
    droneBrand:null,
    droneCode:null,
    droneId:null,
  },
  modalRules: {
    droneBrand: [{ required: true, message: '请选择', trigger: ['change'] }],
    droneCode: [{ required: true, message: '请选择', trigger: ['change'] }],
    droneId: [{ required: true, message: '请选择', trigger: ['change'] }],
  },
  formItem:  [
    {
      type: 'select',
      key: 'droneBrand',
      label: '品牌',
      options: droneBrand,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' },
      rules:[ { required: true, message: '请选择',trigger: ['change'] },]
    },
    {
      type: 'select',
      key: 'droneCode',
      label: '无人机型号',
      options: droneModelCode,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' },
      rules:[ { required: true, message: '请选择',trigger: ['change'] },]
    },
    {
      type: 'select',
      key: 'droneId',
      label: '无人机编码',
      options: droneList,
      row: { sm: 8, md: 8 },
      props: { clearable: true, placeholder: '请选择' },
      rules:[ { required: true, message: '请选择',trigger: ['change'] },]
    },
  ]
})

// 操作记录
export const setpOpions = reactive([
  {
    title:'新增设备',
    description:"七尾 2023.10.10 10:10:00",
    icon:'iconfont icon-chuangjian'
  },
  {
    title:'设备关联：xx无人机',
    description:"七尾领导 2023.10.10 10:10:00",
    icon:'iconfont icon-feiji_qifei'
  },
  {
    title:'解绑设备：xx无人机',
    description:"八尾 2023.10.10 10:10:00",
    icon:'iconfont icon-riqifanwei'
  },
  {
    title:'更改设备状态：报废',
    description:"八尾 2023.10.10 10:10:00",
    icon:'iconfont icon-biaoqian1'
  }
])


// 机场 字典数据类型
export const getType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'airport'}) as any
  const airport_brand = data.find(item=> item.dictType==='airport_brand')?.dateList ||[]
  const airport_model_code = data.find(item=> item.dictType==='airport_model_code')?.dateList ||[]
  const airport_status = data.find(item=> item.dictType==='airport_status')?.dateList?.map(item=>{
    return {
      ...item,
      dictValue:parseInt(item.dictValue)
    }
  })  ||[]
  // 品牌
  airportBrand.value  = dataToSelect(airport_brand, { label: 'dictLabel', value: 'dictValue' }) 
  // 机场型号
  airportModelCode.value = dataToSelect(airport_model_code, { label: 'dictLabel', value: 'dictValue' }) 
  // 可用状态
  airportStatus.value  = dataToSelect(airport_status, { label: 'dictLabel', value: 'dictValue' }) 
}



//  挂载设备类型
export const getMountType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'mount'}) as any
  const mount_type = data.find(item=> item.dictType==='mount_type').dateList ||[]
  const mount_brand = data.find(item=> item.dictType==='mount_brand').dateList ||[]
  mountType.value = dataToSelect(mount_type, { label: 'dictLabel', value: 'dictValue' }) 
  mountBrand.value  = dataToSelect(mount_brand, { label: 'dictLabel', value: 'dictValue' }) 
}




//  挂载型号
export const getSeriesType = async function (prefix) {
  const {code,data=[],msg} = await  findDictBrand({prefix}) as any
      return dataToSelect(data, { label: 'dictLabel', value: 'dictLabel' })
}




//  无人机  字典数据类型
export const setDroneType = async function () {
  const {data=[]} = await  findDictTypeList({prefix:'drone'}) as any
  const drone_brand = data.find(item=> item.dictType==='drone_brand').dateList ||[]
  const drone_model_code = data.find(item=> item.dictType==='drone_model_code').dateList ||[]
  // 品牌
  droneBrand.value  = dataToSelect(drone_brand, { label: 'dictLabel', value: 'dictValue' }) 
  // 无人机型号
  droneModelCode.value = dataToSelect(drone_model_code, { label: 'dictLabel', value: 'dictValue' }) 
}


export const queryDroneList = async function (brand,modelCode) {
  const {data} = await  getDroneList({brand,modelCode}) as any
  droneList.value = dataToSelect(data, { label: 'code', value: 'id' }) 
}